<?php

namespace StudyBuddy;

/**
 * Class represents on record (document)
 * in the Mongo RESOURCE collection
 *
 */
class Resource extends MongoDoc {

    protected $minAutoIncrement = 1;

    public function __construct(Registry $oRegistry) {

        parent::__construct($oRegistry, 'RESOURCE');
    }

    /**
     * Creates new record in RESOURCE collection
     * and returns value of newly created _id
     *
     * @param string $type name of resource type
     *
     * @return int value of new resource id
     */
    public function create($type = null) {
        if (null !== $type) {

            if (is_numeric($type) || !is_string($type)) {
                throw new \InvalidArgumentException('$type must be a string and not a numiric. Was: ' . gettype($typeId) . ' value: ' . $typeId);
            }

            /**
             * Used to be called type_id
             * but we have special rule that 
             * casts anything that ends with _id 
             * into integer
             */
            $this->offsetSet('res_type', $type);
        }

        $this->offsetSet('added_ts', new \MongoDate());

        return $this->save();
    }

}
